/*
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.page.client;

/**
 * The page container filter for changing {@link PageContainer} that depends on different pages.
 * 
 * @author Jess
 * @since 2011/9/23
 */
public abstract class PageContainerFilter implements PageFilter {

  @Override
  public void doPageFilter(String pageName) throws PageFilterException {
    String containerName = this.getContainerName(pageName);
    if (containerName == null) {
      PageContext.setRootPage(null);
    } else if (!containerName.equals(PageContext.getContainerName())) {
      PageContext.setRootPage(containerName, getPageContainer(containerName));
    }
  }

  /**
   * Gets the respective container name of page.
   * 
   * @param page the page
   * @return the respective container name of page
   */
  protected abstract String getContainerName(String page);

  /**
   * Gets the page container for the given name.
   * 
   * @param containerName the container name
   * @return the page container
   */
  protected abstract PageContainer getPageContainer(String containerName);

}
